var login = {
	dialog: null,
	dialog1: null,
	dialog2: null,
	error: "<img style='width: 15px;' src='../images/error.png'/>",
	right: "<img style='width: 15px;' src='../images/right.png'/>",
	init : function(){
		var boss = this;
		boss.initLayout();
		boss.initEvent();
	},
	initLayout : function(){
		
	},
	initEvent : function(){
		var boss = this;
		boss.initForgetPwdDialog();
		boss.initRegisterDialog();
		$("#login_btn").bind("click",function(){
			var username = $("#username").val();
			var password = $("#password").val();
			if(username == "" && password != ""){
				alert("用户名不能为空！");
				return;
			}else if(username != "" && password == ""){
				alert("密码不能为空！");
				return;
			}else if(username == "" && password == ""){
				alert("用户名密码不能为空！");
				return;
			}
			$.ajax({
				dataType: 'json',
				type: 'post',
				data: {username: username, password: password, os: "pc"},
				url: burl + '/User/Login.do',
				success: function(data){
					if(data.success=="true"){
						window.location.href = burl + '/view/main/mapNew.jsp';
					}else{
						alert(data.msg);
					}
				}
			});
		});
		$("#login_div a").bind("mouseenter", function(){
			$(this).css("color", "#ff0000");
		});
		$("#login_div a").bind("mouseleave", function(){
			$(this).css("color", "#0000ff");
		});
		$(window).bind("keydown", function(event){
			if(event.keyCode == 13){
				$("#login_btn").click();
			}
		});
	},
	initRegisterDialog: function(){
		var boss = this;
		$("#register").bind("click", function(){
			boss.dialog = sbl.dialog({
				url: burl + "/view/register.jsp",
				width: "430px",
				height: "540px",
				OnComplete:function(){
					$(".prompt span").css("margin-top", "5px;");
					boss.initUserType();
					boss.initPosition();
					boss.initPcs();
					$("#police_alert, #police_indeed").hide();
					boss.initOrgClick();
					$("#checkMapSpan").bind("click", function(){
						$("#checkMapCode").attr("src", burl + "/Jcaptcha/getImage.do");
					});
					boss.initRegister();
				}
			});
			return false;
		});
	},
	initForgetPwdDialog: function(){
		var boss = this;
		$("#forgetpwd").bind("click", function(){
			boss.dialog1 = sbl.dialog({
				url: burl + "/view/manager/forgetPwd.jsp",
				width: "300px",
				height: "300px",
				OnComplete: function(){
					$("#searchPwd_Btn").bind("click", function(){
						var data = sbl.getFormData("searchPwd_dialog");
						sbl.ajax({
							url: burl + "/User/identityCheck.do",
							data: data,
							success: function(d){
								if(d.success){
									alert(d.msg);
									boss.dialog1.remove();
									boss.initPwdUpdate(d.data);
								}else{
									alert(d.msg);
								}
							}
						});
					});
				}
			});
			return false;
		});
	},
	initPwdUpdate: function(name){
		var boss = this;
		boss.dialog2 = sbl.dialog({
			url: burl + "/view/manager/updatePwd.jsp",
			width: "350px",
			height: "50px",
			OnComplete: function(){
				$("#updatePwd_btn").bind("click", function(){
					var value = $("#newPassword").val();
					sbl.ajax({
						url: burl + "/User/updatePwd.do",
						data: {loginName: name, loginPwd: value},
						success: function(d){
							alert(d.msg);
							boss.dialog2.remove();
						}
					});
				});
			}
		});
	},
	initUserType: function(){
		var userType = modelDao.getUserType();
		for(var i = 0; i < userType.length; i++){
			var item = userType[i];
			var option = $("<option></option>").html(item.NAME).attr("value",item.ID);
			$("#user_type").append(option);
		}
		
		//同步手机号码为帐号
		$("#user_phone").blur(function(){
			var phone=$("#user_phone").val();
			$("#login_name").val(phone);
		});
	},
	initPosition: function(){
		var data = modelDao.getPosition();
		var htmlContent = "";
		for(var i = 0; i < data.length; i++){
			var row = data[i];
			htmlContent += "<option value='" + row.ID + "'>" + row.POSITION_NAME + "</option>";
		}
		$("#mj_position").html(htmlContent);
	},
	initPcs: function(){
		var data = modelDao.getDept(1);
		var htmlContent = "<option value=''>-请选择-</option>";
		for(var i = 0; i < data.length; i++){
			var row = data[i];
			htmlContent += "<option value='" + row.id + "'>" + row.deptname + "</option>";
		}
		$("#police_station").html(htmlContent);
	},
	initJq: function(id){
		$("#police_alert").empty();
		var htmlContent = "<option value=''>-警区-</option>";
		if(id == ""){
			
		}else{
			var data = modelDao.getDept(id);
			for(var i = 0; i < data.length; i++){
				var row = data[i];
				htmlContent += "<option value='" + row.id + "'>" + row.deptname + "</option>";
			}
		}
		$("#police_alert").append(htmlContent);
	},
	initZrq: function(id){
		$("#police_indeed").empty();
		var htmlContent = "<option value=''>-责任区-</option>";
		if(id == ""){
			
		}else{
			var data = modelDao.getDept(id);
			for(var i = 0; i < data.length; i++){
				var row = data[i];
				htmlContent += "<option value='" + row.id + "'>" + row.deptname + "</option>";
			}
		}
		$("#police_indeed").append(htmlContent);
	},
	initOrgClick: function(){
		var boss = this;
		$("#police_station").bind("change", function(){
			var pcs = $(this).find("option:selected").html();
			var val = $(this).val();
			if(val == ""){
				$("#pcs_span, #jq_span, #zrq_span").empty();
				$("#police_alert, #police_indeed").hide();
			}else{
				$("#pcs_span").html(pcs);
				$("#jq_span, #zrq_span").empty();
				$("#police_alert").show();
				$("#police_indeed").hide();
				boss.initJq(val);
			}
		});
		$("#police_alert").bind("change", function(){
			var jq = $(this).find("option:selected").html();
			var val = $(this).val();
			if(val == ""){
				$("#jq_span, #zrq_span").empty();
				$("#police_indeed").hide();
			}else{
				$("#jq_span").html(jq);
				$("#zrq_span").empty();
				$("#police_indeed").show();
				boss.initZrq(val);
			}
		});
		$("#police_indeed").bind("change", function(){
			var zrq = $(this).find("option:selected").html();
			var val = $(this).val();
			if(val == ""){
				$("#zrq_span").empty();
			}else{
				$("#zrq_span").html(zrq);
			}
		});
	},
	initRegister: function(){
		var boss = this;
		$("#register_img").bind("mouseenter", function(){
			$(this).attr("src", burl + "/images/register_btn_enter.png");
		});
		$("#register_img").bind("mouseleave", function(){
			$(this).attr("src", burl + "/images/register_btn.png");
		});
		$("input[name='usertype']").bind("click", function(){
			var type = $(this).val();
			//人员身份的判断
			if(type == "5"){
				$("#reg_jh, #reg_zw, #reg_dw, #reg_dept").hide();
			}else{
				$("#reg_jh, #reg_zw, #reg_dw, #reg_dept").show();
			}
		});
		$("#register_img").bind("click", function(){
			var data = sbl.getFormData("register_dialog");
			var checkPwd = $("#check_pwd").val();
			var map = $("#checkMap").val();
			var pcs = $("#police_station").val();
			var jq = $("#police_alert").val();
			var zrq = $("#police_indeed").val();
//			//校验用户账号
//			if(!common.isTrueLoginName(data.username)){
//				$("#login_name_lable").html(boss.error);
//				return;
//			}else{
//				$("#login_name_lable").html(boss.right);
//			}
			//校验密码
			if(!common.isTruePwd(data.password, checkPwd)){
				$("#pwd_m").html(boss.error);
				return;
			}else{
				$("#pwd_msg").html(boss.right);
			}
			//校验姓名
			if(!common.isTrueValueNotNull(data.showname)){
				$("#name_msg").html(boss.error);
				return;
			}else{
				$("#name_msg").html(boss.right);
			}
			//校验手机号码
			if(!common.isTrueTelNo(data.telNumber)){
				$("#phone_msg").html(boss.error);
				return;
			}else{
				$("#phone_msg").html(boss.right);
			}
//			//校验身份证号码
//			if(!common.isTrueCardId(data.cardId)){
//				$("#card_msg").html(boss.error);
//				return;
//			}else{
//				$("#card_msg").html(boss.right);
//			}
//			//根据身份证号码校验用户性别
//			if(data.sex != common.isTrueCardidToSex(data.cardId)){
//				$("#sex_msg, #card_msg").html(boss.error);
//				return;
//			}else{
//				$("#sex_msg, #card_msg").html(boss.right);
//			}
//			//校验银行卡号是否为空
//			if(!common.isTrueValueNotNull(data.bankcardNo)){
//				$("#bankcard_msg").html(boss.error);
//			}else{
//				$("#bankcard_msg").html(boss.right);
//			}
			//校验验证码是否一致
			if(!common.isTrueMap(map)){
				$("#checkMapSpan").html(boss.error);
				return;
			}else{
				$("#checkMapSpan").html(boss.right);
			}

			data.jq = jq;
			data.pcs = pcs;
			data.zrq = zrq;
			
			var d = modelDao.register(data);
			alert(d.msg);
			boss.dialog.remove();
			if(d.success){
				window.location.href = burl + "/view/main/mapNew.jsp";
			}
		});
	}
};
$(function(){
	login.init();
});